
import pymysql
# 需要安装pymysql 
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", passwd="123456", database="jwsy")
 
cursor = db.cursor()
 
# 传输过来的数据替换掉1120191234
sql = "SELECT SNAME FROM student WHERE SID= '1120191234'" 
try:
    n = 0
    # 执行SQL语句
    cursor.execute(sql)
    result = cursor.fetchone()

    while result is not None:
        sname = ''.join(result)
        print(sname)  # 这里选择了输出，需要改成调用或者赋值
        result = cursor.fetchone()
        n = 1
    if n == 0:
        print("NULL")  # 没有数据输出NULL
    else:
        sql = "SELECT SCLASS FROM student WHERE SID= '1120191234'"
        cursor.execute(sql)
        result = cursor.fetchone()
        while result is not None:
            sclass = ''.join(result)
            print(sclass)  # 这里选择了输出
            result = cursor.fetchone()


except cursor.DoseNotExcept:
    print("Error: unable to fetch data")

 
# 关闭数据库连接
db.close()